<!DOCTYPE HTML>
<html>
<head>
    {include file="common:meta"}
</head>
<body>
<div class="page-container">
    <div class=" float-e-margins">
        <div class="ibox-content">
            <div class="tabs-container">
                <ul class="nav nav-tabs">

                    <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">扣除列表</a>
                    </li>
                    <li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="true">扣除钻石</a>
                    </li>

                </ul>
                <div class="tab-content">

                    <div class=" tab-pane active" id="tab-1">
                        <form action="" class="form-inline" data-url="">
                            <div class="form-group">
                                <label class="control-label" for="key">选项:</label>
                                <select id="key" class="form-control" name="key">
                                    <option value="u_id">ID</option>
                                    <option value="admin">管理员</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label class="control-label" for="value">输入:</label>
                                <input class="form-control" id="value" type="text" name="value">
                            </div>
                            <button type="button" class="btn btn-primary" id="eventQuery">查询</button>
                            <button type="button" onclick="location.replace(location.href);" class="btn btn-success ">刷新</button>
                        </form>
                        <table id="tb_departments"></table>
                    </div>
                    <div class="tab-pane" id="tab-2">
                        <div class="ibox-content">

                            <form class="form-horizontal"  id="form">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="user_id">
                                        <span class="c-red">*</span>ID：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input type="number" class="form-control" id="user_id" value="" name="user_id">
                                    </div>
                                </div>

                                <div id="where_area">

                                    <div class="form-group">
                                        <label class="col-sm-2 control-label" for="prize_number">
                                            <span id="custom" class="c-red">*扣除数量：</span>
                                        </label>
                                        <div class="col-md-2 col-sm-10">
                                            <input type="number" class="form-control" id="prize_number" step="1" min="0"  name="prize_number">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="remark">
                                        <span class="c-red">*原因必填：</span>
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <textarea name="remark" id="remark"  cols="50" rows="8">
                                        </textarea>
                                    </div>
                                </div>


                                <div class="form-group" id="form2">
                                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                                        <button id="cp_btn" class="btn btn-primary" type="button">扣除
                                        </button>
                                    </div>
                                </div>
                                <!--<div class="form-group">-->
                                <!--<label class="col-sm-2 control-label" for="account">-->
                                <!--<span class="c-red">*赠送类型：</span>-->
                                <!--</label>-->
                                <!--<div class="col-md-2 col-sm-10">-->

                                <!--<select class="form-control" name="account" id="account">-->
                                <!--<option value="活动赠送">活动赠送</option>-->
                                <!--<option value="活跃市场">活跃市场</option>-->
                                <!--<option value="官方后台冲值">官方后台冲值</option>-->
                                <!--<option value="官方账户发放奖品预存">官方账户发放奖品预存</option>-->
                                <!--<option value="其他">其他</option>-->
                                <!--<option value="酥酥语音大额充值">酥酥语音大额充值</option>-->
                                <!--</select>-->
                                <!--</div>-->
                                <!--</div>-->


                            </form>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
{include file="common:footer"}

<script>

    $(document).ready(function () {
        $('#prize_number').keyup(function(){
            $(this).val($(this).val().replace(/\D|^0/g,''));
        }).bind("paste",function(){
            return false;
        });




        initTable();
        // initTable3();
        $('#cp_btn').on('click',function(){


            var that = this;
            var iuser_id = document.getElementById('user_id');
            var iprize_number = document.getElementById('prize_number');
            var iremark = document.getElementById('remark');
            if(iuser_id.value == ""){
                layer.msg('ID不能为空', { icon: 2, time: 2000});
                return false;
            }if(iprize_number.value == ""){
                layer.msg('扣除数量不能为空', { icon: 2, time: 2000});
                return false;
            }if(iremark.value.trim() == ""){
                layer.msg('原因不能为空', { icon: 2, time: 2000});
                return false;
            }


            var finalRes = $("#form").serializeArray().reduce(function(result, item){
                result[item.name] = item.value;
                return result;
            }, {});

            $(that).removeAttr('disabled').removeClass('disabled');
            $.ajax({
                url: '/GiveRecordCheck',
                type: 'POST',
                dataType: 'json',
                data:JSON.stringify(finalRes),
                processData: false,
                contentType: "application/json",
                cache: false,
                async: false,
                beforeSend: function(){
                    $(that).attr("disabled","disabled").addClass('disabled');
                },
                success: function (re) {


                    if (re.code === 100) {
                        layer.msg(re.msg, {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒
                        }, function () {
                            $(that).removeAttr('disabled').removeClass('disabled');
                            // re.url ? window.location.href = re.url : '';
                        });
                        //  window.location.href=document.referrer;
                    } else {
                        layer.msg(re.msg,{ icon: 2,time: 2000});
                        $(that).removeAttr('disabled').removeClass('disabled');
                    }
                },
                error: function () {
                    $(that).removeAttr('disabled').removeClass('disabled');
                },
                complete: function () {
                    $(that).removeAttr('disabled').removeClass('disabled');
                }
            });
        });





    });

    function initTable() {
        $('#tb_departments').bootstrapTable({
            url: '/GiveRecordIndexData',         //请求后台的URL（*）
            dataField: "data",
            method: 'get',                      //请求方式（*）
            striped: true,                      //是否显示行间隔色
            cache: true,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            queryParams: queryParams,//传递参数（*）
            showToggle: true,
            showRefresh: true,
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: true,                  //是否显示所有的列
            showPaginationSwitch: true,
            minimumCountColumns: 2,             //最少允许的列数

            columns: [{
                field: 'u_id',
                title: 'ID'
            }, {
                field: 'nickname',
                title: '用户昵称'
            }, {
                field: 'prize_type',
                title: '奖品类型'
            },{
                field: 'gift_name',
                title: '奖品名称'
            }, {
                field: 'prize_number',
                title: '奖品数量(或天数)'
            },{
                field: 'remark',
                title: '扣除原因'
            }, {
                field: 'create_time',
                title: '扣除时间',
                sortable: true
            }]
        });
    }
    //得到查询的参数
    function queryParams(params) {
        params.offset = params.offset / params.limit + 1;
        params.key = $('#key').val();
        params.value = $('#value').val();
        console.log(JSON.stringify(params));
        return params;
    }

    function operateFormatter(value, row, index) {
    }
    // 索按钮触发事件
    $(function () {
        $("#eventQuery").click(function () {
            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
        });

        $('#account').on('click',function (e) {
            var val = $(this).val();
            if(val == '酥酥语音大额充值'){
                $('#gf').removeClass('hidden');
                $('#where_area').addClass('hidden');
                $('#btn2').addClass('hidden');
                $('#cp_btn2').removeClass('hidden');
            }else{
                $('#gf').addClass('hidden');
                $('#where_area').removeClass('hidden');
                $('#btn2').removeClass('hidden');
                $('#cp_btn2').addClass('hidden');
            }
        });
    });

    $('#prize_type').change(function(){
        var prize_type = $(this).val();
        if(prize_type == 3)
            $('#custom').html('*奖励天数：');
        else
            $('#custom').html('*奖励数量：');
        console.log($('#custom').text());

        $.ajax({
            type: 'get',
            dataType: 'json',
            url: '/GivePresenGiftName',
            data: {'prize_type': prize_type},
            success: function (re) {
                if (re.code === 100) {

                    console.log(re);
                    var htmlText = "<option value='' selected>--请选择--</option>";
                    if(re.data !== null){

                        for(var i in re.data){

                            if(prize_type == 4)
                                htmlText+="<option value="+ re.data[i]['id']+">"+ re.data[i]['name']+'--'+re.data[i]['diamonds']+"</option>";
                            else
                                htmlText+="<option value="+ re.data[i]['id']+">"+ re.data[i]['name']+"</option>";
                        }

                        $('#prize_id').html(htmlText);
                    }
                }else{
                    layer.msg(re.msg,{
                        icon:2,
                        time:1500
                    });
                }
            },
            error: function (data) {
                layer.msg('服务器出小差了',{
                    icon:2,
                    time:1500
                });
            }
        });
    });

    $('#key').change(function(){

        if($(this).val() === 'account'){
            var html = '<select id="value" class="form-control" name="value">' +
                '<option value="活动赠送">活动赠送</option>' +
                '<option value="活跃市场">活跃市场</option>' +
                '<option value="官方后台冲值">官方后台冲值</option>' +
                '<option value="官方账户发放奖品预存">官方账户发放奖品预存</option>' +
                '<option value="其他">其他</option>' +
                '<option value="酥酥语音大额充值">酥酥语音大额充值</option>' +
                '</select>';
            $('#value').replaceWith(html);
        }else
            $('#value').replaceWith('<input class="form-control" id="value" type="text" name="value">');

    });
    window.operateEvents = {

        'click .build-union':function(e,value,row,index){
            var that = this;

            layer.open({
                type: 2,
                title: ['审批','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: false,
                shade: true,
                maxmin: false, //开启最大化最小化按钮
                area: ['100%', '100%'],
                content:"/GivePresenApproveView?id=" + that.dataset.bid,  //type=1 批量禁封 不是则单个禁封
                offset:'auto',
                scrollbar:false,
                end: function () {

                    // $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
                }
            });

        }

    };

</script>
</body>
</html>